package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.userInfo.adapter.MaterialsAuthenticationAdapter;
import com.rongshu.view.userInfo.entity.PostVipAuthentication;
import com.rongshu.view.userInfo.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MaterialsAuthenticationActivity extends BaseActivity implements OnItemClickListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.txt_top_function)
    TextView txt_top_function;
    @BindView(R.id.rv_inPrint)
    RecyclerView rv_inPrint;
    @BindView(R.id.rv_paperMedia)
    RecyclerView rv_paperMedia;
    @BindView(R.id.rv_newMedia)
    RecyclerView rv_newMedia;
    @BindView(R.id.rv_carryPrize)
    RecyclerView rv_carryPrize;
    @BindView(R.id.rv_otherPlatform)
    RecyclerView rv_otherPlatform;
    //intent
    private Intent intent = null;
    //request
    private final int PUBLISH_WORKS = 0x01;
    private final int PAGER_MEDIA_WORKS = 0x02;
    private final int NEW_MEDIA_WORKS = 0x03;
    private final int CARRY_OFF_A_PRIZE = 0x04;
    private final int OTHER_PLATFORM_AUTHENTICATION_ID = 0x05;
    //已出版作品
    private PostVipAuthentication.VipDateModeBean inPrintBean = null;
    private PostVipAuthentication.VipDateModeBean paperMediaBean = null;
    private PostVipAuthentication.VipDateModeBean newMediaBean = null;
    private PostVipAuthentication.VipDateModeBean carryPrizeBean = null;
    private PostVipAuthentication.VipDateModeBean otherPlatformBean = null;
    //上传拼接的资料认证信息
    private PostVipAuthentication authentication = null;
    //list
    private List<PostVipAuthentication.VipDateModeBean> inPrintList = null;
    private List<PostVipAuthentication.VipDateModeBean> paperMediaList = null;
    private List<PostVipAuthentication.VipDateModeBean> newMediaList = null;
    private List<PostVipAuthentication.VipDateModeBean> carryPrizeList = null;
    private List<PostVipAuthentication.VipDateModeBean> otherPlatformList = null;
    //adapter
    private MaterialsAuthenticationAdapter inPrintAdapter = null;
    private MaterialsAuthenticationAdapter paperMediaAdapter = null;
    private MaterialsAuthenticationAdapter newMediaAdapter = null;
    private MaterialsAuthenticationAdapter carryPrizeAdapter = null;
    private MaterialsAuthenticationAdapter otherPlatformAdapter = null;
    //返回局部更新position
    private int position = -1;

    @Override
    protected int initLayout() {
        return R.layout.activity_materials_authentication_view;
    }

    @Override
    protected void initView() {
        //title
        txt_top_title.setText("资料认证");
        //function
        txt_top_function.setVisibility(View.VISIBLE);
        txt_top_function.setText("确定");
        //list
        inPrintList = new ArrayList<>();
        paperMediaList = new ArrayList<>();
        newMediaList = new ArrayList<>();
        carryPrizeList = new ArrayList<>();
        otherPlatformList = new ArrayList<>();
        //adapter
        inPrintAdapter = new MaterialsAuthenticationAdapter(this);
        inPrintAdapter.setOnItemClickListener(this);
        paperMediaAdapter = new MaterialsAuthenticationAdapter(this);
        paperMediaAdapter.setOnItemClickListener(this);
        newMediaAdapter = new MaterialsAuthenticationAdapter(this);
        newMediaAdapter.setOnItemClickListener(this);
        carryPrizeAdapter = new MaterialsAuthenticationAdapter(this);
        carryPrizeAdapter.setOnItemClickListener(this);
        otherPlatformAdapter = new MaterialsAuthenticationAdapter(this);
        otherPlatformAdapter.setOnItemClickListener(this);
        //set adapter
        inPrintAdapter.setVipDateModeBeanList(inPrintList);
        paperMediaAdapter.setVipDateModeBeanList(paperMediaList);
        newMediaAdapter.setVipDateModeBeanList(newMediaList);
        carryPrizeAdapter.setVipDateModeBeanList(carryPrizeList);
        otherPlatformAdapter.setVipDateModeBeanList(otherPlatformList);
        //recyclerview
        rv_inPrint.setLayoutManager(new LinearLayoutManager(this));
        rv_inPrint.setAdapter(inPrintAdapter);
        rv_paperMedia.setLayoutManager(new LinearLayoutManager(this));
        rv_paperMedia.setAdapter(paperMediaAdapter);
        rv_newMedia.setLayoutManager(new LinearLayoutManager(this));
        rv_newMedia.setAdapter(newMediaAdapter);
        rv_carryPrize.setLayoutManager(new LinearLayoutManager(this));
        rv_carryPrize.setAdapter(carryPrizeAdapter);
        rv_otherPlatform.setLayoutManager(new LinearLayoutManager(this));
        rv_otherPlatform.setAdapter(otherPlatformAdapter);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        authentication = UserInfoUtils.getVipAuthentication();
        if (authentication != null) {
            List<PostVipAuthentication.VipDateModeBean> vipDateModeBeanList = authentication.getVipDateMode();
            if (vipDateModeBeanList != null) {
                for (PostVipAuthentication.VipDateModeBean bean : vipDateModeBeanList) {
                    int type = bean.getType();
                    switch (type) {
                        case 1:
                            rv_inPrint.setVisibility(View.VISIBLE);
                            inPrintList.add(bean);
                            inPrintAdapter.notifyDataSetChanged();
                            break;
                        case 2:
                            rv_paperMedia.setVisibility(View.VISIBLE);
                            paperMediaList.add(bean);
                            paperMediaAdapter.notifyDataSetChanged();
                            break;
                        case 3:
                            rv_newMedia.setVisibility(View.VISIBLE);
                            newMediaList.add(bean);
                            newMediaAdapter.notifyDataSetChanged();
                            break;
                        case 4:
                            rv_carryPrize.setVisibility(View.VISIBLE);
                            carryPrizeList.add(bean);
                            carryPrizeAdapter.notifyDataSetChanged();
                            break;
                        case 5:
                            rv_otherPlatform.setVisibility(View.VISIBLE);
                            otherPlatformList.add(bean);
                            otherPlatformAdapter.notifyDataSetChanged();
                            break;
                    }
                }
            }
        }
    }

    @OnClick({R.id.layout_back, R.id.layout_publish_works, R.id.layout_paper_media_works,
            R.id.layout_new_media_works, R.id.layout_carry_off_a_prize, R.id.layout_others_platform_authentication_id,
            R.id.txt_top_function})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
            case R.id.layout_publish_works://已出版作品
                gistType(1, null);
                break;
            case R.id.layout_paper_media_works://纸媒发表作品
                gistType(2, null);
                break;
            case R.id.layout_new_media_works://新媒体平台作品
                gistType(3, null);
                break;
            case R.id.layout_carry_off_a_prize://获奖情况
                gistType(4, null);
                break;
            case R.id.layout_others_platform_authentication_id://其他平台加V认证ID
                gistType(5, null);
                break;
            case R.id.txt_top_function://提交
                if (inPrintBean == null && paperMediaBean == null && newMediaBean == null && carryPrizeBean == null && otherPlatformBean == null) {
                    toast("请先上传资料认证");
                    return;
                }
                authentication = new PostVipAuthentication();
                List<PostVipAuthentication.VipDateModeBean> beanList = new ArrayList<>();
                if (!inPrintList.isEmpty()) {
                    beanList.addAll(inPrintList);
                }
                if (!paperMediaList.isEmpty()) {
                    beanList.addAll(paperMediaList);
                }
                if (!newMediaList.isEmpty()) {
                    beanList.addAll(newMediaList);
                }
                if (carryPrizeList != null) {
                    beanList.addAll(carryPrizeList);
                }
                if (otherPlatformList != null) {
                    beanList.addAll(otherPlatformList);
                }
                authentication.setVipDateMode(beanList);
                intent = new Intent();
                intent.putExtra(Constant.INTENT_DATA, authentication);
                setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }

    /**
     * 根据type
     */
    private void gistType(int type, PostVipAuthentication.VipDateModeBean bean) {
        intent = new Intent(this, AddProductionActivity.class);
        switch (type) {
            case 1:
                intent.putExtra(Constant.INTENT_TYPE, 1);
                intent.putExtra(Constant.INTENT_TITLE, "作品名称");
                intent.putExtra(Constant.INTENT_MESSAGE, "ISBN号");
                if (bean != null) {
                    intent.putExtra(Constant.INTENT_DATA, bean);
                } else {
                    position = -1;
                }
                startActivityForResult(intent, PUBLISH_WORKS);
                break;
            case 2:
                intent.putExtra(Constant.INTENT_TYPE, 2);
                intent.putExtra(Constant.INTENT_TITLE, "作品名称");
                intent.putExtra(Constant.INTENT_MESSAGE, "发表刊物名称");
                if (bean != null) {
                    intent.putExtra(Constant.INTENT_DATA, bean);
                } else {
                    position = -1;
                }
                startActivityForResult(intent, PAGER_MEDIA_WORKS);
                break;
            case 3:
                intent.putExtra(Constant.INTENT_TYPE, 3);
                intent.putExtra(Constant.INTENT_TITLE, "作品名称");
                intent.putExtra(Constant.INTENT_MESSAGE, "新媒体平台名称");
                if (bean != null) {
                    intent.putExtra(Constant.INTENT_DATA, bean);
                } else {
                    position = -1;
                }
                startActivityForResult(intent, NEW_MEDIA_WORKS);
                break;
            case 4:
                intent.putExtra(Constant.INTENT_TYPE, 4);
                intent.putExtra(Constant.INTENT_TITLE, "奖项");
                intent.putExtra(Constant.INTENT_MESSAGE, "ISBN号");
                if (bean != null) {
                    intent.putExtra(Constant.INTENT_DATA, bean);
                } else {
                    position = -1;
                }
                startActivityForResult(intent, CARRY_OFF_A_PRIZE);
                break;
            case 5:
                intent.putExtra(Constant.INTENT_TYPE, 5);
                intent.putExtra(Constant.INTENT_TITLE, "加V ID");
                intent.putExtra(Constant.INTENT_MESSAGE, "平台名称");
                if (bean != null) {
                    intent.putExtra(Constant.INTENT_DATA, bean);
                } else {
                    position = -1;
                }
                startActivityForResult(intent, OTHER_PLATFORM_AUTHENTICATION_ID);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PUBLISH_WORKS://已出版作品
                    inPrintBean = (PostVipAuthentication.VipDateModeBean) data.getSerializableExtra(Constant.INTENT_DATA);
                    if (inPrintBean == null) {
                        return;
                    }
                    if (position == -1) {
                        rv_inPrint.setVisibility(View.VISIBLE);
                        inPrintList.add(inPrintBean);
                        inPrintAdapter.notifyDataSetChanged();
                    } else {
                        inPrintList.set(position, inPrintBean);
                        inPrintAdapter.notifyItemRangeChanged(position, 1, "refresh");
                    }
                    break;
                case PAGER_MEDIA_WORKS://纸媒发表作品
                    paperMediaBean = (PostVipAuthentication.VipDateModeBean) data.getSerializableExtra(Constant.INTENT_DATA);
                    if (paperMediaBean == null) {
                        return;
                    }
                    if (position == -1) {
                        rv_paperMedia.setVisibility(View.VISIBLE);
                        paperMediaList.add(paperMediaBean);
                        paperMediaAdapter.notifyDataSetChanged();
                    } else {
                        paperMediaList.set(position, paperMediaBean);
                        paperMediaAdapter.notifyItemRangeChanged(position, 1, "refresh");
                    }
                    break;
                case NEW_MEDIA_WORKS://新媒体平台作品
                    newMediaBean = (PostVipAuthentication.VipDateModeBean) data.getSerializableExtra(Constant.INTENT_DATA);
                    if (newMediaBean == null) {
                        return;
                    }
                    if (position == -1) {
                        rv_newMedia.setVisibility(View.VISIBLE);
                        newMediaList.add(newMediaBean);
                        newMediaAdapter.notifyDataSetChanged();
                    } else {
                        newMediaList.set(position, newMediaBean);
                        newMediaAdapter.notifyItemRangeChanged(position, 1, "refresh");
                    }
                    break;
                case CARRY_OFF_A_PRIZE://获奖情况
                    carryPrizeBean = (PostVipAuthentication.VipDateModeBean) data.getSerializableExtra(Constant.INTENT_DATA);
                    if (carryPrizeBean == null) {
                        return;
                    }
                    if (position == -1) {
                        rv_carryPrize.setVisibility(View.VISIBLE);
                        carryPrizeList.add(carryPrizeBean);
                        carryPrizeAdapter.notifyDataSetChanged();
                    } else {
                        carryPrizeList.set(position, carryPrizeBean);
                        carryPrizeAdapter.notifyItemRangeChanged(position, 1, "refresh");
                    }
                    break;
                case OTHER_PLATFORM_AUTHENTICATION_ID://其他平台加V认证ID
                    otherPlatformBean = (PostVipAuthentication.VipDateModeBean) data.getSerializableExtra(Constant.INTENT_DATA);
                    if (otherPlatformBean == null) {
                        return;
                    }
                    if (position == -1) {
                        rv_otherPlatform.setVisibility(View.VISIBLE);
                        otherPlatformList.add(otherPlatformBean);
                        otherPlatformAdapter.notifyDataSetChanged();
                    } else {
                        otherPlatformList.set(position, otherPlatformBean);
                        otherPlatformAdapter.notifyItemRangeChanged(position, 1, "refresh");
                    }
                    break;
            }
        }
    }

    /**
     * item click
     *
     * @param type
     * @param position
     */
    @Override
    public void onClick(int type, int position, PostVipAuthentication.VipDateModeBean bean) {
        this.position = position;
        gistType(type, bean);
    }
}
